import { defineStore } from 'pinia'
import { ref } from 'vue'

const useTagsViewStore = defineStore('tagsView', () => {
  const visitedViews = ref([])
  const cachedViews = ref([])
  const iframeViews = ref([])

  /**
   * 添加菜单记录
   */
  const addVisitedView = (view) => {
    if (visitedViews.value.some((v) => v.path === view.path)) return
    const data = Object.assign({}, view, { title: view.meta.title || 'no-name' })
    visitedViews.value.push(data)
  }

  /**
   * 添加缓存记录
   */
  const addCachedView = (view) => {
    if (cachedViews.value.includes(view.name)) return
    if (!view.meta.noCache) {
      cachedViews.value.push(view.name)
    }
  }

  /**
   * 添加内联记录
   */
  const addIframeView = (view) => {
    if (iframeViews.value.some((v) => v.path === view.path)) return
    iframeViews.value.push(Object.assign({}, view, { title: view.meta.title || 'no-name' }))
  }

  /**
   * 删除菜单记录
   */
  const delVisitedView = (view) => {
    return new Promise((resolve) => {
      for (const [i, v] of visitedViews.value.entries()) {
        if (v.path === view.path) {
          visitedViews.value.splice(i, 1)
          break
        }
      }
      iframeViews.value = iframeViews.value.filter((item) => item.path !== view.path)
      resolve(visitedViews.value)
    })
  }

  /**
   * 删除缓存记录
   */
  const delCachedView = (view) => {
    return new Promise((resolve) => {
      const index = cachedViews.value.indexOf(view.name)
      index > -1 && cachedViews.value.splice(index, 1)
      resolve(cachedViews.value)
    })
  }

  /**
   * 删除内联记录
   */
  const delIframeView = (view) => {
    return new Promise((resolve) => {
      iframeViews.value = iframeViews.value.filter((item) => item.path !== view.path)
      resolve(iframeViews.value)
    })
  }

  /**
   * 添加菜单记录和缓存记录
   */
  const addView = (view) => {
    addVisitedView(view)
    addCachedView(view)
  }

  /**
   * 删除菜单记录和缓存记录
   */
  const delView = (view) => {
    return delVisitedView(view)
      .then(() => delCachedView(view))
      .then(() => {
        return { visitedViews: visitedViews.value, cachedViews: cachedViews.value }
      })
  }

  /**
   * 删除其他菜单记录
   */
  const delOthersVisitedViews = (view) => {
    visitedViews.value = visitedViews.value.filter((v) => (v.meta.affix || v.path) === view.path)
    iframeViews.value = iframeViews.value.filter((item) => item.path === view.path)
    return Promise.resolve(visitedViews.value)
  }

  /**
   * 删除其他缓存记录
   */
  const delOthersCachedViews = (view) => {
    const index = cachedViews.value.indexOf(view.name)
    if (index > -1) {
      cachedViews.value = cachedViews.value.slice(index, index + 1)
    } else {
      cachedViews.value = []
    }
    return Promise.resolve(cachedViews.value)
  }

  /**
   * 删除所有菜单记录
   */
  const delAllVisitedViews = () => {
    visitedViews.value = visitedViews.value.filter((tag) => tag.meta.affix)
    iframeViews.value = []
    return Promise.resolve(visitedViews.value)
  }

  /**
   * 删除所有缓存记录
   */
  const delAllCachedViews = () => {
    cachedViews.value = []
    return Promise.resolve(cachedViews.value)
  }

  /**
   * 删除所有记录
   */
  const delAllViews = () => {
    return delAllVisitedViews().then(() => delAllCachedViews())
  }

  /**
   * 更新菜单记录
   */
  const updateVisitedView = (view) => {
    const index = visitedViews.value.findIndex((v) => v.path === view.path)
    visitedViews.value[index] = Object.assign({}, visitedViews.value[index], view)
  }

  /**
   * 删除右侧记录
   */
  const delRightTags = (view) => {
    const index = visitedViews.value.findIndex((v) => v.path === view.path)
    if (index === -1) return
    visitedViews.value = visitedViews.value.filter((item, idx) => {
      if (idx <= index || (item.meta && item.meta.affix)) {
        return true
      }
      const i = cachedViews.value.indexOf(item.name)
      if (i > -1) {
        cachedViews.value.splice(i, 1)
      }
      if (item.meta.link) {
        const fi = iframeViews.value.findIndex((v) => v.path === item.path)
        iframeViews.value.splice(fi, 1)
      }
      return false
    })
    return Promise.resolve(visitedViews.value)
  }

  /**
   * 删除左侧记录
   */
  const delLeftTags = (view) => {
    const index = visitedViews.value.findIndex((v) => v.path === view.path)
    if (index === -1) return
    visitedViews.value = visitedViews.value.filter((item, idx) => {
      if (idx >= index || (item.meta && item.meta.affix)) {
        return true
      }
      const i = cachedViews.value.indexOf(item.name)
      if (i > -1) {
        cachedViews.value.splice(i, 1)
      }
      if (item.meta.link) {
        const fi = iframeViews.value.findIndex((v) => v.path === item.path)
        iframeViews.value.splice(fi, 1)
      }
      return false
    })
    return Promise.resolve(visitedViews.value)
  }

  return {
    visitedViews,
    cachedViews,
    iframeViews,
    addVisitedView,
    addCachedView,
    addIframeView,
    delVisitedView,
    delCachedView,
    delIframeView,
    addView,
    delView,
    delOthersVisitedViews,
    delOthersCachedViews,
    delAllVisitedViews,
    delAllCachedViews,
    delAllViews,
    updateVisitedView,
    delRightTags,
    delLeftTags
  }
})

export default useTagsViewStore
